package com.example.lib.java;

import static java.util.Comparator.comparing;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author YT_FLY
 */
public class Java8 {

    public static void main(String[] args) {
        List<Apple> list = new ArrayList<>();
        list.add(new Apple(1));
        list.add(new Apple(4));
        list.add(new Apple(3));
        list.sort(comparing(Apple::getWeight));
        System.out.println(list.toString());

        List<Apple> lists = list.stream()
                .filter((Apple a) -> a.getWeight() >= 3)
                .collect(Collectors.toList());

        List<Apple> listss = list.stream()
                .filter((Apple a) -> a.getWeight() >= 3)
                .map((Apple a) -> {
                    return new Apple(a.getWeight() * a.getWeight());
                })
                .collect(Collectors.toList());

        Optional<Apple> listsss = lists
                .stream()
                .filter((Apple a) -> a.getWeight() == 3)
                .findAny();
        System.out.println(listsss.toString());


        System.out.println(listss.toString());
        Collections.reverse(lists);
        System.out.println(lists.toString());

        List<Integer> numList = new ArrayList<>();
        numList.add(1);
        numList.add(1);
        numList.add(1);
        numList.add(1);
        numList.add(1);

        Optional result = numList.stream()
                .reduce((sun, a) -> {
                    sun = sun + a;
                    return sun;
                });
        System.out.println(result);

        feibonaqi();
    }

    public static class Apple {


        int weight;

        public int getWeight() {
            return weight;
        }

        public Apple(int weight) {
            this.weight = weight;

        }

        public void setWeight(int weight) {
            this.weight = weight;
        }

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder("{");
            sb.append("\"weight\":")
                    .append(weight);
            sb.append('}');
            return sb.toString();


        }


    }

    private static void feibonaqi() {
        Stream.iterate(new int[]{0, 1},
                t -> new int[]{t[1], t[0]+t[1]})
                .limit(20)
                .forEach(t -> System.out.println("(" + t[0] + "," + t[1] +")"));
    }
}
